package com.jfreer.mybatis.expand.javamapper.elements;

import com.jfreer.mybatis.expand.IntrospectedTableMyBatis3Freework;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/**
 * User: landy
 * Date: 14-6-28
 * Time: 下午6:40
 */
public class DeleteInBatchMethodGenerator extends AbstractJavaMapperMethodGenerator {
    @Override
    public void addInterfaceElements(Interface interfaze) {
        if(introspectedTable.getPrimaryKeyColumns().size()!=1){
            /**
             * 当主键数量不为1时，不生成该方法。
             * a.没有主键
             * b.复合主键
             */
            return;
        }
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        importedTypes.add(FullyQualifiedJavaType.getNewListInstance());

        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setName(((IntrospectedTableMyBatis3Freework) introspectedTable).getDeleteInBatchStatementId());

        FullyQualifiedJavaType parameterType = FullyQualifiedJavaType
                .getNewListInstance();
        FullyQualifiedJavaType pkType;
        pkType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();

        importedTypes.add(pkType);
        parameterType.addTypeArgument(pkType);

        method.addParameter(new Parameter(parameterType, "ids")); //$NON-NLS-1$


        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }
}
